package com.sun.servlet;

import com.alibaba.fastjson.JSON;
import com.sun.dao.FineDao;
import com.sun.domain.FineUtil;
import com.sun.domain.Reader;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author SunRui
 * @description fine servlet
 * @date 2024/7/4 21:09
 */
@WebServlet(urlPatterns = "/FineServlet.action")
public class FineServlet extends HttpServlet {
    private static Logger logger = Logger.getLogger(ReaderServlet.class);
    InputStream resource = Resources.getResourceAsStream("mybatisConfig.xml");
    //获取session工厂对象
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);

    public FineServlet() throws IOException {
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取session会话对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        FineDao fineDao = sqlSession.getMapper(FineDao.class);
        HttpSession session = req.getSession();
        String opr = req.getParameter("opr");
        String str = null;
        String jsonString = null;
        Reader reader;
        int readerId;
        switch (opr){
            case "fineTabShow":
                //查询罚款缴纳记录
                int page = Integer.parseInt(req.getParameter("page"));
                logger.debug("查询罚款起始页数:"+page);
                reader = (Reader) session.getAttribute("reader");
                readerId = reader.getId();
                List<FineUtil> fineUtil =  fineDao.getFineList(((page-1)*5),readerId);
                sqlSession.close();
                jsonString = JSON.toJSONString(fineUtil);
                resp.getWriter().write(jsonString);
                break;
            case "finePageShow":
                //获取总页数
                reader = (Reader) session.getAttribute("reader");
                readerId = reader.getId();
                int allPage = fineDao.getAllPage(readerId);
                int pages;
                if (allPage%5>0){
                    pages = (allPage/5)+1;
                }else {
                    pages = allPage/5;
                }
                sqlSession.close();
                jsonString = JSON.toJSONString(pages);
                resp.getWriter().write(jsonString);
                break;
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

}
